package com.nf.health.app.activity.hostopal;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.hostopal.adapter.MyHopitalListAdapter;
import com.nf.health.app.activity.hostopal.model.MyHopstipalListBaen;
import com.nf.health.app.activity.hostopal.view.ItemLinearView;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.customview.EmptyView;
import com.nf.health.app.customview.SimpleSwipeRefreshLayout;
import com.nf.health.app.http.Act;
import com.nf.health.app.http.Apis;
import com.nf.health.app.models.MengHistoryModel;
import com.nf.health.app.utils.Utility;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 *我的医院
 */
public class MyHostipalListActivity extends BaseActivity implements View.OnClickListener {
    public static final String TAG_MYLISTVIEW = "com.nf.mylist.sends";
   @Bind(R.id.dlvlistview_myhostipal)
   DragListView dlvlistview_myhostipal;
   @Bind(R.id.ll_service_hostipal_other)
   LinearLayout ll_service_hostipal_other;

    @Bind(R.id.swipe_myhostipal_layout)
    SimpleSwipeRefreshLayout swipe_myhostipal_layout;
    private MyHopitalListAdapter myHopitalListAdapter;
    private List<MyHopstipalListBaen> list;


    LinearLayout ll_myservice_hospital;
    private LinearLayout ll_wb_hospital;
    private ImageView iv_other_histipal_list;
    private RelativeLayout rl_other_histipal_list;
    private int pageNo = 1;
    private boolean isPullToRefreshed = true;//是不是上拉刷新还是下拉加载
    private LayoutInflater layoutInflater;
    private    ArrayList<MengHistoryModel>  mengHistoryModelList;
    private EmptyView emptyView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_myhostipallistactivity);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "我的医院");
        initView();
        initListener();
        loadData(pageNo);
        Apis.HID = "";//增加强壮行，将我的医院列表详情的hid置空
    }

    private void initView() {
        layoutInflater = LayoutInflater.from(MyHostipalListActivity.this);
        list = new ArrayList<>();
        swipe_myhostipal_layout.setViewGroup(dlvlistview_myhostipal);
        dlvlistview_myhostipal.setRefreshableAndLoadMoreable(false, true);
        swipe_myhostipal_layout.setColorSchemeResources(R.color.theme_color, android.R.color.holo_red_light, android.R.color.holo_green_light, android.R.color.holo_orange_light);
        myHopitalListAdapter = new MyHopitalListAdapter(this, list);
       /*View headView =  View.inflate(this,R.layout.myhostipal_head,null);*/

        ll_wb_hospital = (LinearLayout) findViewById(R.id.ll_wb_hospital);
        iv_other_histipal_list = (ImageView) findViewById(R.id.iv_other_histipal_list);
        rl_other_histipal_list = (RelativeLayout) findViewById(R.id.rl_other_histipal_list);
        ll_myservice_hospital = (LinearLayout) findViewById(R.id.ll_myservice_hospital);
        /*LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,50);
        headView.setLayoutParams(params);
        headView.setTextColor(ContextCompat.getColor(this,R.color.black9));*/
        /*dlvlistview_myhostipal.addHeaderView(headView);*/
        dlvlistview_myhostipal.setAdapter(myHopitalListAdapter);
        rl_other_histipal_list.setVisibility(View.GONE);
        mengHistoryModelList = (ArrayList<MengHistoryModel>) getIntent().getSerializableExtra(TAG_MYLISTVIEW);
        emptyView = new EmptyView(MyHostipalListActivity.this)
                .setOnRefreshData(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        loadData(pageNo);
                    }
                });
        if(mengHistoryModelList == null || mengHistoryModelList.size() == 0){
            ll_wb_hospital.setVisibility(View.GONE);
        }else{
            ll_wb_hospital.setVisibility(View.VISIBLE);
            for (int i = 0; i < mengHistoryModelList.size(); i++) {
                MengHistoryModel mengHistoryModel = mengHistoryModelList.get(i);
                ItemLinearView itemLinearView = new ItemLinearView(this);
                itemLinearView.loadData(mengHistoryModel);
                ll_myservice_hospital.addView(itemLinearView);
            }
        }

    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        switch (actionName){
            case Act.Flag.FLAG_QUERY_MY_DOCOTOR_HOSPITAL:
                List<MyHopstipalListBaen> subListSearchMainBean =( List<MyHopstipalListBaen>) object;
                dlvlistview_myhostipal.completeLoadMore();
                if(isPullToRefreshed){
                    list.clear();
                    list.addAll(subListSearchMainBean);
                    if(subListSearchMainBean.size() < 20){
                        dlvlistview_myhostipal.setRefreshableAndLoadMoreable(false,false);
                    }else{
                        dlvlistview_myhostipal.setRefreshableAndLoadMoreable(false,true);
                    }
                    if(list.size() > 0){
                        rl_other_histipal_list.setVisibility(View.VISIBLE);

                        if(mengHistoryModelList == null || mengHistoryModelList.size() == 0){
                            iv_other_histipal_list.setTag(R.drawable.iv_open_arrow);
                            iv_other_histipal_list.setBackgroundResource(R.drawable.iv_open_arrow);
                            dlvlistview_myhostipal.setVisibility(View.VISIBLE);//服务医院没有，要显示出来
                        }else{
                            iv_other_histipal_list.setTag(R.drawable.iv_close_arrow);
                            iv_other_histipal_list.setBackgroundResource(R.drawable.iv_close_arrow);
                            dlvlistview_myhostipal.setVisibility(View.GONE);//显示起来
                        }

                    }else{
                        rl_other_histipal_list.setVisibility(View.GONE);
                        if(mengHistoryModelList == null || mengHistoryModelList.size() == 0){
                            ll_service_hostipal_other.addView(emptyView);
                        }
                    }
                }else{
                    list.addAll(subListSearchMainBean);
                    if(subListSearchMainBean.size() < 20){
                        dlvlistview_myhostipal.setRefreshableAndLoadMoreable(false,false);
                    }else{
                        dlvlistview_myhostipal.setRefreshableAndLoadMoreable(false,true);
                    }
                }
                myHopitalListAdapter.setDoctorCommentData(list);
                swipe_myhostipal_layout.setRefreshing(false);
                Utility.setListViewHeightBasedOnChildren(dlvlistview_myhostipal);
                break;
        }
    }

    /**
     * 获取搜索到的数据
     */
    private void loadData(int pageNo){
         provider.queryMyHospitalList(pageNo+"","20");
    }
    private void initListener() {
        iv_other_histipal_list.setOnClickListener(this);
        rl_other_histipal_list.setOnClickListener(this);
        dlvlistview_myhostipal.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {

            }
            @Override
            public void onLoadMore() {
                isPullToRefreshed = false;
                pageNo++;
                loadData(pageNo);
            }
        });
        swipe_myhostipal_layout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;//表示消费了
            }
        });
        swipe_myhostipal_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                loadData(pageNo);
            }
        });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.iv_other_histipal_list:
            case R.id.rl_other_histipal_list:
                Integer integer = (Integer) iv_other_histipal_list.getTag();
                if(integer == R.drawable.iv_close_arrow){
                    iv_other_histipal_list.setTag(R.drawable.iv_open_arrow);
                    iv_other_histipal_list.setBackgroundResource(R.drawable.iv_open_arrow);
                    dlvlistview_myhostipal.setVisibility(View.VISIBLE);
                }else if(integer == R.drawable.iv_open_arrow){
                    iv_other_histipal_list.setTag(R.drawable.iv_close_arrow);
                    iv_other_histipal_list.setBackgroundResource(R.drawable.iv_close_arrow);
                    dlvlistview_myhostipal.setVisibility(View.GONE);
                }
                break;
        }
    }
}